//
//  VideoFeedCell.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/7/17.
//  Copyright © 2020 lt. All rights reserved.
//

import Foundation
import UIKit
import AVFoundation.AVPlayerItem
import RxSwift
import RxCocoa
import Lottie
import MediaPlayer
import UIKit
import Alamofire
import MediaPlayer
import AVKit
import MBProgressHUD

protocol VideoFeedCellTouchDelegate: AnyObject {
    func downLoadTouch(trillEntity:TrillEntity)
    func copyTextBtnTouch(trillEntity:TrillEntity)
    func shareBtnTouch(trillEntity:TrillEntity)
}

class VideoFeedCell: UITableViewCell {
    private var playImage: UIImageView!
    
    private var playerView: DYPlayerView!
    
    private var viewModel:VideoCellViewModel?
    
    private var shareBtn:VideoFeedCellBtn!
    
    private var copyTextBtn:VideoFeedCellBtn!
    
    private var donwLoadBtn:VideoFeedCellBtn!
    
    private var collectionBtn:VideoFeedCellBtn!
    
    private var likeBtn:VideoFeedCellBtn!
    
    private var goodsDetailsCard:UIView!
    
    private var dyVideoLabel:UILabel!
    
    private var goodsDetailsImage:UIImageView!
    
    private var itemShortTitle:UILabel!
    
    private var shoptypeIcon:UIImageView!
    
    private var itemEndPriceLabel:UILabel!
        
    private var couponMoneyLabel:UILabel!
    
    private var couponMoneyBgView:UIView!
    
    private var itemPrice:UILabel!
    
    private var shareEarningsButton:UIButton!
    
    private var buyEarningsButton:UIButton!
    
    weak var delegate: VideoFeedCellTouchDelegate?
        
    private(set) var isReadyToPlay: Bool = false
    
    private var bag:DisposeBag = DisposeBag()
    
    public var startPlayOnReady: (() -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //当前已经被分配的cell如果被重用了(通常是滚动出屏幕外了),会调用cell的prepareForReuse通知cell
    override func prepareForReuse() {
        super.prepareForReuse()
        self.imageView?.isHidden = false
        self.isReadyToPlay = false
//        self.playProgressView.setProgress(0, animated: false)
        bag = DisposeBag()
        print("cell重写-->prepareForReuse")
    }
    
    
    //数据绑定
    public func bind(viewModel: VideoCellViewModel) {
        self.viewModel = viewModel
        self.playerView.viewModel = viewModel
        
        viewModel.playUrl.bind(to: self.playerView.rx.playUrl).disposed(by: bag)
        
        viewModel.status.observeOn(MainScheduler.instance)
            .subscribe(onNext: { [weak self] in
                self?.playImageAnimation(status: $0)})
            .disposed(by: bag)
        
        viewModel.status.accept(.none)
        
        viewModel.dyVideoLikeCount
            .asDriver()
            .drive(onNext: { [weak self](count) in
                guard let `self` = self else {return}
                self.likeBtn.label.text = "\(count)"
            })
            .disposed(by: bag)
        
        viewModel.isLikedStatus
            .asDriver()
            .drive(onNext: { [weak self](isLike) in
                guard let `self` = self else {return}
                if isLike {
                    self.likeBtn.imageView.image = UIImage(named: "icon_like_btn_S")
                }else {
                    self.likeBtn.imageView.image = UIImage(named: "icon_like_btn")
                }})
            .disposed(by: bag)
        
        viewModel.isCollectionStatus.asDriver().drive(onNext: { [weak self](status) in
              guard let `self` = self else {return}
            if(status){
                self.collectionBtn.imageView.image = UIImage(named: "icon_collection_btn_S")
            }else{
                 self.collectionBtn.imageView.image = UIImage(named: "icon_collection_btn")
            }
        }).disposed(by: bag)
        
        viewModel.dyVideoTitle.drive(dyVideoLabel.rx.text).disposed(by: bag)
        
        viewModel.itemShortTitle.drive(itemShortTitle.rx.text).disposed(by: bag)
        
        viewModel.couponMoney.bind { [weak self] in
           guard let `self` = self else { return }
           self.couponMoneyLabel.text = $0
        }.disposed(by: bag)
        
        viewModel.itemPrice1.bind { [weak self] in
           guard let `self` = self else { return }
           self.shareEarningsButton.setTitle("分享赚¥\($0 ?? "")", for: .normal)
        }.disposed(by: bag)
        
        viewModel.itemPrice2.bind { [weak self] in
           guard let `self` = self else { return }
           self.buyEarningsButton.setTitle("分享赚¥\($0 ?? "")", for: .normal)
         }.disposed(by: bag)
        
        viewModel.itemEndPrice.bind { [weak self] in
            guard let `self` = self else { return }
            self.setItemEndPriceLabel($0)
        }.disposed(by: bag)
        
        viewModel.itemPic.bind{ [weak self] in
            guard let `self` = self else { return }
            self.goodsDetailsImage.kf.setImage(urlString: "\($0!)_120x120.jpg")
        }.disposed(by: bag)
        
        viewModel.itemPrice.bind{ [weak self] in
            guard let `self` = self else { return }
            self.setItemPrice($0 ?? "")
        }.disposed(by: bag)

        viewModel.shopType.bind{ [weak self] in
            guard let `self` = self else { return }
            if($0 == "B"){
                  self.shoptypeIcon.image = UIImage(named: "icon_tianMao")
            }else{
                  self.shoptypeIcon.image = UIImage(named: "icon_taoBao")
            }

        }.disposed(by: bag)
        
    }
    
    public func play() {
        playerView.play()
    }
    
    // 播放/暂停 按钮动画
    private func playImageAnimation(status: DYPlayerStatus) {
        switch status {
        case .pause:
            self.playImage.isHidden = false
            self.playImage.alpha = 0
            self.playImage.transform = CGAffineTransform(scaleX: 1.8, y: 1.8)
            UIView.animate(withDuration: 0.15, animations: {
                self.playImage.alpha = 1
                self.playImage.transform = CGAffineTransform(scaleX: 1, y: 1)
            })
        case .playing:
            UIView.animate(withDuration: 0.15, animations: {
                self.playImage.alpha = 0
            }, completion: { _ in
                self.playImage.isHidden = false
            })
            
        case .none:
            
            self.playImage.isHidden = true
        }
    }

}

// UI 相关方法
extension VideoFeedCell{
    private func setUpUI() {
        self.selectionStyle = .none
        contentView.backgroundColor = UIColor.clear
        addPlayerView()
        topBgView()
        bottomBgView()
        addPlayImage()
        addShareBtn()
        addCopyTextBtn()
        addDownloadBtn()
        addCollectionBtn()
        addLikeBtn()
        addGoodsDetailsCard()
        addDyVideoLabel()
    }
    
    func addPlayerView() {
        playerView = DYPlayerView()
        playerView.delegate = self
        contentView.addSubview(playerView)
        playerView.translatesAutoresizingMaskIntoConstraints = false
        playerView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
        playerView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
        playerView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
        playerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
    }
    
    func topBgView(){
        let layerView = UIView()
        layerView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 144)
        let layer = CALayer()
        layer.frame = layerView.bounds
        layer.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0).cgColor
        layerView.layer.addSublayer(layer)
        let gradient1 = CAGradientLayer()
        gradient1.colors = [UIColor(red: 0, green: 0, blue: 0, alpha: 0.3).cgColor, UIColor(red: 0, green: 0, blue: 0, alpha: 0).cgColor]
        gradient1.locations = [0, 1]
        gradient1.startPoint = CGPoint(x: 0.5, y: 0)
        gradient1.endPoint = CGPoint(x: 0.5, y: 1)
        gradient1.frame = layerView.bounds
        layerView.layer.addSublayer(gradient1)
        layerView.isUserInteractionEnabled = false
    
        contentView.addSubview(layerView)
    }
    
    func bottomBgView(){
        let layerView = UIView()
        layerView.frame = CGRect(x: 0, y: screenHeight-288.2, width: screenWidth, height: 288.2)
        let layer = CALayer()
        layer.frame = layerView.bounds
        layer.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0).cgColor
        layerView.layer.addSublayer(layer)
        let gradient1 = CAGradientLayer()
        gradient1.colors = [UIColor(red: 0, green: 0, blue: 0, alpha: 0.3).cgColor, UIColor(red: 0, green: 0, blue: 0, alpha: 0).cgColor]
        gradient1.locations = [0, 1]
        gradient1.startPoint =  CGPoint(x: 0.5, y: 1)
        gradient1.endPoint = CGPoint(x: 0.5, y: 0)
        gradient1.frame = layerView.bounds
        layerView.layer.addSublayer(gradient1)
        layerView.isUserInteractionEnabled = false
        contentView.addSubview(layerView)
    }

    
    func addPlayImage() {
        playImage = UIImageView()
        playImage.image = UIImage(named: "icon_play_pause52x62")
        contentView.addSubview(playImage)
        playImage.translatesAutoresizingMaskIntoConstraints = false
        playImage.centerXAnchor.constraint(equalTo: contentView.centerXAnchor, constant: 0).isActive = true
        playImage.centerYAnchor.constraint(equalTo: contentView.centerYAnchor, constant: 0).isActive = true
    }
    
    func addShareBtn(){
        shareBtn = VideoFeedCellBtn()
        shareBtn.imageView.image = UIImage(named: "icon_share_btn")
        shareBtn.label.text = "分享"
        contentView.addSubview(shareBtn)
        shareBtn.translatesAutoresizingMaskIntoConstraints = false
        shareBtn.bottomAnchor.constraint(equalTo: contentView.bottomAnchor,constant:-screenHeight*(118.5/667)).isActive = true
        shareBtn.rightAnchor.constraint(equalTo: contentView.rightAnchor,constant: -12).isActive = true
        shareBtn.widthAnchor.constraint(equalToConstant: (35/375)*screenWidth).isActive = true
         shareBtn.heightAnchor.constraint(equalToConstant: (47/375)*screenWidth).isActive = true
        let tapGesture = UITapGestureRecognizer{ [weak self](gesture) in
                    guard let `self` = self, let viewModel = self.viewModel else { return }
                   self.delegate?.shareBtnTouch(trillEntity:viewModel.trillEntity.value)
               }
        shareBtn.addGestureRecognizer(tapGesture)
    }
    
    func addCopyTextBtn(){
        copyTextBtn = VideoFeedCellBtn()
        copyTextBtn.imageView.image = UIImage(named: "icon_copy_label")
        copyTextBtn.label.text = "文案"
        contentView.addSubview(copyTextBtn)
        copyTextBtn.translatesAutoresizingMaskIntoConstraints = false
        copyTextBtn.centerXAnchor.constraint(equalTo: shareBtn.centerXAnchor,constant: 0).isActive = true
        copyTextBtn.bottomAnchor.constraint(equalTo: shareBtn.topAnchor,constant: -17).isActive = true
        shareBtn.widthAnchor.constraint(equalToConstant: (35/375)*screenWidth).isActive = true
         shareBtn.heightAnchor.constraint(equalToConstant: (47/375)*screenWidth).isActive = true
        let tapGesture = UITapGestureRecognizer{ [weak self](gesture) in
             guard let `self` = self, let viewModel = self.viewModel else { return }
            self.delegate?.copyTextBtnTouch(trillEntity:viewModel.trillEntity.value)
        }
        copyTextBtn.addGestureRecognizer(tapGesture)
    }
    
    func addDownloadBtn(){
        donwLoadBtn = VideoFeedCellBtn()
        donwLoadBtn.imageView.image = UIImage(named: "icon_download_btn")
        donwLoadBtn.label.text = "下载"
        contentView.addSubview(donwLoadBtn)
        donwLoadBtn.translatesAutoresizingMaskIntoConstraints = false
        donwLoadBtn.centerXAnchor.constraint(equalTo: copyTextBtn.centerXAnchor,constant: 0).isActive = true
        donwLoadBtn.bottomAnchor.constraint(equalTo: copyTextBtn.topAnchor,constant: -17).isActive = true
        shareBtn.widthAnchor.constraint(equalToConstant: (35/375)*screenWidth).isActive = true
         shareBtn.heightAnchor.constraint(equalToConstant: (47/375)*screenWidth).isActive = true
        let tapGesture = UITapGestureRecognizer{ [weak self](gesture) in
             guard let `self` = self, let viewModel = self.viewModel else { return }
            self.delegate?.downLoadTouch(trillEntity:viewModel.trillEntity.value)
        }
        donwLoadBtn.addGestureRecognizer(tapGesture)
    }
    
    
    func addCollectionBtn(){
        collectionBtn = VideoFeedCellBtn()
        collectionBtn.imageView.image = UIImage(named: "icon_collection_btn")
        collectionBtn.label.text = "收藏"
        contentView.addSubview(collectionBtn)
        collectionBtn.translatesAutoresizingMaskIntoConstraints = false
        collectionBtn.centerXAnchor.constraint(equalTo: donwLoadBtn.centerXAnchor,constant: 0).isActive = true
        collectionBtn.bottomAnchor.constraint(equalTo: donwLoadBtn.topAnchor,constant: -17).isActive = true
        shareBtn.widthAnchor.constraint(equalToConstant: (35/375)*screenWidth).isActive = true
         shareBtn.heightAnchor.constraint(equalToConstant: (47/375)*screenWidth).isActive = true
        let tapGesture = UITapGestureRecognizer{ [weak self](gesture) in
            guard let `self` = self, let viewModel = self.viewModel else { return }
            viewModel.postCollectionStatus()
        }
        collectionBtn.addGestureRecognizer(tapGesture)
    }
    
    func addLikeBtn(){
        likeBtn = VideoFeedCellBtn()
        likeBtn.imageView.image = UIImage(named: "icon_like_btn")
        contentView.addSubview(likeBtn)
        likeBtn.translatesAutoresizingMaskIntoConstraints = false
        likeBtn.centerXAnchor.constraint(equalTo: collectionBtn.centerXAnchor,constant: 0).isActive = true
        likeBtn.bottomAnchor.constraint(equalTo: collectionBtn.topAnchor,constant: -15).isActive = true
        shareBtn.widthAnchor.constraint(equalToConstant: (35/375)*screenWidth).isActive = true
         shareBtn.heightAnchor.constraint(equalToConstant: (47/375)*screenWidth).isActive = true
        let tapGesture = UITapGestureRecognizer {[weak self](gesture) in
            guard let `self` = self, let viewModel = self.viewModel else { return }
            viewModel.postLikedStatus()
        }
        likeBtn.addGestureRecognizer(tapGesture)
    }
    
    //注意设置圆角造成离屏渲染 参考链接https://www.meiwen.com.cn/subject/kxzszftx.html
    func addGoodsDetailsCard(){
        goodsDetailsCard = UIView()
        goodsDetailsCard.layer.backgroundColor = UIColor.white.cgColor
        goodsDetailsCard.layer.cornerRadius = 10
        goodsDetailsCard.frame = CGRect(x: (12/375)*screenWidth, y: (544.5/667)*screenHeight, width: (305/375)*screenWidth, height: (109/375)*screenWidth)
        contentView.addSubview(goodsDetailsCard)
        addGoodsDetailsImage()
        addShoptypeIcon()
        addTitle()
        addItemEndPriceLabel()
        addCouponMoneyLabel()
        addItemPrice()
        addShareEarningsButton()
        addBuyEarningsButton()
    }
    
    
    
    func addDyVideoLabel(){
        dyVideoLabel =  UILabel(text:"",font:.systemFont(ofSize:13))
        dyVideoLabel.textColor = .white
        dyVideoLabel.numberOfLines = 3
        contentView.addSubview(dyVideoLabel)
        dyVideoLabel.translatesAutoresizingMaskIntoConstraints = false
        dyVideoLabel.bottomAnchor.constraint(equalTo: goodsDetailsCard.topAnchor,constant:-9).isActive = true
        dyVideoLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor,constant: 12).isActive = true
        dyVideoLabel.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 253.5/375).isActive = true
    }
    
}

//MARK: - 商品item UI
extension VideoFeedCell{
    //设置券后价富文本
    private func setItemEndPriceLabel(_ endPrice:String?){
        let attrString = NSMutableAttributedString(string: "券后￥\(endPrice ?? "0.00")" )
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang-SC-Regular", size: 11)!,.foregroundColor: UIColor(red: 0.95, green: 0.13, blue: 0.13,alpha:1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        let strSubAttr3: [NSMutableAttributedString.Key: Any] = [.font: UIFont(name: "PingFang-SC-Medium", size: 18)!,.foregroundColor: UIColor(red: 0.95, green: 0.13, blue: 0.13,alpha:1)]
        attrString.addAttributes(strSubAttr3, range: NSRange(location: 3, length: attrString.length-3))
        itemEndPriceLabel.attributedText = attrString
    }
    
    //图片这样设置不会离屏渲染  参考链接https://www.meiwen.com.cn/subject/kxzszftx.html
    private func addGoodsDetailsImage(){
        goodsDetailsImage = UIImageView()
        goodsDetailsImage.contentMode = .scaleAspectFit
        goodsDetailsImage.layer.cornerRadius = 10
        goodsDetailsImage.layer.masksToBounds = true
        goodsDetailsImage.frame = CGRect(x: (7/375)*screenWidth, y: (goodsDetailsCard.frame.size.height-(95/375)*screenWidth)/2, width: (95/375)*screenWidth, height: (95/375)*screenWidth)
        goodsDetailsCard.addSubview(goodsDetailsImage)
    }
    
    private func addShoptypeIcon(){
        shoptypeIcon = UIImageView()
        shoptypeIcon.contentMode = .scaleAspectFit
        shoptypeIcon.layer.cornerRadius = 2
        shoptypeIcon.layer.masksToBounds = true
        shoptypeIcon.backgroundColor = .blue
        shoptypeIcon.frame = CGRect(x: goodsDetailsImage.frame.maxX+(10/375)*screenWidth, y: (7/375)*screenWidth, width: (13/375)*screenWidth, height: (13/375)*screenWidth)
        goodsDetailsCard.addSubview(shoptypeIcon)
    }
    
    
    private func addTitle(){
        itemShortTitle = UILabel(text: "", font: .systemFont(ofSize: 14))
        itemShortTitle.textColor = UIColor(r: 102, g: 102, b: 102, a: 1)
        itemShortTitle.numberOfLines = 1
        itemShortTitle.textAlignment = .left
        itemShortTitle.frame = CGRect(x: shoptypeIcon.frame.maxX + (4/375)*screenWidth, y: shoptypeIcon.frame.minY, width: (152/375)*screenWidth, height: 13)
        goodsDetailsCard.addSubview(itemShortTitle)
    }
    
    private func addItemEndPriceLabel(){
        itemEndPriceLabel = UILabel()
        itemEndPriceLabel.frame = CGRect(x: shoptypeIcon.frame.minX, y: shoptypeIcon.frame.maxY+screenWidth*(9/375), width: (100/375)*screenWidth, height: 15)
        goodsDetailsCard.addSubview(itemEndPriceLabel)
    }
    
    private func addCouponMoneyLabel(){
        let layerView = UIView()
        layerView.frame = CGRect(x: goodsDetailsCard.frame.maxX - (30/375)*screenWidth - (53/375)*screenWidth , y:itemEndPriceLabel.frame.minY+(itemEndPriceLabel.frame.height-screenWidth*(17/375))/2, width: (53/375)*screenWidth, height: (17/375)*screenWidth)
        goodsDetailsCard.addSubview(layerView)
        
        let layer = CALayer()
        layer.frame = layerView.bounds
        layer.backgroundColor = UIColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 1).cgColor
        layerView.layer.addSublayer(layer)
        let gradient1 = CAGradientLayer()
        gradient1.colors = [UIColor(red: 0.67, green: 0.44, blue: 1, alpha: 1).cgColor, UIColor(red: 0.44, green: 0.33, blue: 1, alpha: 1).cgColor]
        gradient1.locations = [0, 1]
        gradient1.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradient1.endPoint = CGPoint(x: 1.0, y: 0.5)
        gradient1.frame = layerView.bounds
        layerView.layer.addSublayer(gradient1)
        layerView.layer.cornerRadius = 2;
        layerView.layer.masksToBounds = true
        
        couponMoneyLabel = UILabel()
        couponMoneyLabel.textColor = .white
        couponMoneyLabel.textAlignment = .center
        couponMoneyLabel.font = .systemFont(ofSize: 13)
        layerView.addSubview((couponMoneyLabel))
        couponMoneyLabel.frame = CGRect(x:0 , y: 0, width: (53/375)*screenWidth, height: (17/375)*screenWidth)
    }
    
    func addItemPrice(){
        itemPrice = UILabel()
        itemPrice.frame = CGRect(x: shoptypeIcon.frame.minX, y: itemEndPriceLabel.frame.maxY+(6/375)*screenWidth, width: 100, height: 11)
        itemPrice.numberOfLines = 0
        goodsDetailsCard.addSubview(itemPrice)
    }
    
    func setItemPrice(_ price:String){
        let attrString = NSMutableAttributedString(string: "原价￥\(price)")
        let attr: [NSAttributedString.Key : Any] = [
            .font: UIFont(name: "PingFang-SC-Regular", size: 12)!,
            .foregroundColor: UIColor(red: 0.6, green: 0.6, blue: 0.6,alpha:1),
            .strikethroughStyle: NSUnderlineStyle.single.rawValue,
            .strikethroughColor: UIColor(red: 0.6, green: 0.6, blue: 0.6,alpha:1),
        ]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        itemPrice.attributedText = attrString
    }
    
    //分享赚
    func addShareEarningsButton(){
        shareEarningsButton = UIButton(type: .custom)
        shareEarningsButton.frame = CGRect(x: shoptypeIcon.x, y: (75/375)*screenWidth, width: (90/375)*screenWidth, height: (24/375)*screenWidth)
        goodsDetailsCard.addSubview(shareEarningsButton)
        let gradient1 = CAGradientLayer()
        gradient1.colors = [UIColor(red: 1, green: 0.7, blue: 0.23, alpha: 1).cgColor, UIColor(red: 1, green: 0.56, blue: 0, alpha: 1).cgColor]
        gradient1.locations = [0, 1]
        gradient1.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradient1.endPoint = CGPoint(x: 1.0, y: 0.5)
        gradient1.frame = shareEarningsButton.bounds
        shareEarningsButton.layer.addSublayer(gradient1)
        shareEarningsButton.layer.cornerRadius = 12;
        shareEarningsButton.layer.masksToBounds = true
        shareEarningsButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
    }
    
    //购买返
    func addBuyEarningsButton(){
        buyEarningsButton = UIButton(type: .custom)
        buyEarningsButton.frame = CGRect(x: shareEarningsButton.frame.maxX + (6/375)*screenWidth, y: (75/375)*screenWidth, width: (90/375)*screenWidth, height: (24/375)*screenWidth)
        goodsDetailsCard.addSubview(buyEarningsButton)
        let gradient1 = CAGradientLayer()
        gradient1.colors = [UIColor(red: 1, green: 0.34, blue: 0.2, alpha: 1).cgColor, UIColor(red: 0.95, green: 0.19, blue: 0.19, alpha: 1).cgColor]
        gradient1.locations = [0, 1]
        gradient1.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradient1.endPoint = CGPoint(x: 1.0, y: 0.5)
        gradient1.frame = buyEarningsButton.bounds
        buyEarningsButton.layer.addSublayer(gradient1)
        buyEarningsButton.layer.cornerRadius = 12
        buyEarningsButton.layer.masksToBounds = true
        buyEarningsButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
    }
}

extension VideoFeedCell: DYPlayerViewDelegate {
    func onItemCurrentTimeChange(current: Float64, duration: Float64) {
    }
    
    func onItemStatusChange(status: AVPlayerItem.Status) {
        switch status {
        case .readyToPlay:
            isReadyToPlay = true
            startPlayOnReady?()
        default:
            break
        }
    }
}
